package cn.edu.sasu;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import cn.edu.sasu.domain.Users;
import cn.edu.sasu.webservice.MeService;
import cn.edu.sasu.webservice.impl.MeServiceImpl;

//投票
public class MeActivity extends Activity {

	private MeService meService = null;

	private EditText username = null;
	private EditText address = null;

	private LinearLayout userinfoLinearLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.me);
		setTheme(android.R.style.Theme_Translucent_NoTitleBar);

		userinfoLinearLayout = (LinearLayout) findViewById(R.id.userinfoLinearLayout);

		username = (EditText) findViewById(R.id.username);
		address = (EditText) findViewById(R.id.address);

		meService = new MeServiceImpl();

		if (meService.isLoginUser()) {// 表示用户已经登录
			username.setText(Users.getUser().getUserName());
			address.setText(Users.getUser().getUserAddress());
		} else {
			userinfoLinearLayout.removeAllViews();
			Toast.makeText(this, R.string.no_login, Toast.LENGTH_SHORT).show();
		}

	}

	private long inbacktime = 0;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if ((System.currentTimeMillis() - inbacktime) > 2000) {// 如果两次按键时间间隔大于2000毫秒，则不退出
				Toast.makeText(this, R.string.doubleexit, Toast.LENGTH_SHORT)
						.show();

				inbacktime = System.currentTimeMillis();// 更新inbacktime
			} else {
				System.exit(0);// 否则退出程序
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}